import cv2
import os
from PIL import Image



# 人脸数据收集

face_detector = cv2.CascadeClassifier('./static/haarcascade/haarcascade_frontalface_default.xml')

face_id = input('\n 输入用户id:')

# im = Image.open('./static/face_data_login/20220214174254.png').convert('L')  # 打开图像，并转成灰度图像
# img11=Image.fromarray(uint8(im))

img = cv2.imread('static/face_data_login/20220214174254.png')
# print(img)
# 转为灰度图片
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 检测人脸
faces = face_detector.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+w), (255, 0, 0))
    # 保存图像
    cv2.imwrite("./static/face_data_register/User." + str(face_id) + '.jpg', gray[y: y + h, x: x + w])
    # cv2.imshow('image', img)
